package com.loeng.module.system.controller.admin.permission;

import com.loeng.framework.common.enums.CommonStatusEnum;
import com.loeng.framework.common.pojo.CommonResult;
import com.loeng.framework.common.util.object.BeanUtils;
import com.loeng.module.system.controller.admin.permission.vo.menu.MenuListReqVO;
import com.loeng.module.system.controller.admin.permission.vo.menu.MenuSimpleRespVO;
import com.loeng.module.system.dal.dataobject.permission.MenuDO;
import com.loeng.module.system.service.permission.MenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;

import static com.loeng.framework.common.pojo.CommonResult.success;

/**
 * 菜单 API Controller
 * @author loeng zhuo
 */
@Tag(name = "管理后台 - 菜单")
@RestController
@RequestMapping("/system/menu")
@Validated
public class MenuController {

    @Resource
    private MenuService menuService;

    @GetMapping({"/list-all-simple", "simple-list"})
    @Operation(summary = "获取菜单精简信息列表", description = "只包含被开启的菜单，用于【角色分配菜单】功能的选项。" +
            "在多租户的场景下，会只返回租户所在套餐有的菜单")
    public CommonResult<List<MenuSimpleRespVO>> getSimpleMenuList() {
        List<MenuDO> list = menuService.getMenuListByTenant(
                MenuListReqVO.builder().status(CommonStatusEnum.ENABLE.getStatus()).build()
        );
        list = menuService.filterDisableMenus(list);
        list.sort(Comparator.comparing(MenuDO::getSort));
        return success(BeanUtils.toBean(list, MenuSimpleRespVO.class));
    }

}
